/**
 * 专利监控管理
 * @author 牧羊人
 * @since 2022/01/07
 */
layui.use(['dropdown','form','function','table'], function () {
    //声明变量
    var func = layui.function
        , table = layui.table
        , layer = layui.layer
        , form = layui.form
        , dropdown = layui.dropdown
        , $ = layui.$;

    if (A == 'index') {
        //【TABLE列数组】
        var cols = [
            {type: 'checkbox', fixed: 'left'}
            , {field: 'company_name', width: 180, title: '公司名称', align: 'center'}
            , {field: 'patent_name', width: 180, title: '[自缴] 专利名称', align: 'center', templet: function (d) {             
                    if (d.self_pay==1) {
                        return "[是] "+d.patent_name;
                    }else{
                        return "[否] "+d.patent_name;
                    }
                }
            }
            , {field: 'year', width: 100, title: '第N年', align: 'center'}
            , {field: 'number', width: 100, title: '案件编号', align: 'center'}
            , {field: 'type', width: 100, title: '类型', align: 'center', templet: function (d) {             
                    if (d.type==1) {
                        return "年费";
                    }else{
                        return "登印费";
                    }
                }
            }
            , {field: 'apply_time_str', width: 130, title: '申请日期', align: 'center'}
            , {field: 'issue_time_str', width: 130, title: '发文日期', align: 'center'}
            , {field: 'pay_time_str', width: 130, title: '缴费日期', align: 'center'}
            , {field: 'status', width: 100, title: '状态', align: 'center', templet: function (d) {             
                    if (d.status==1) {
                        return "正常";
                    }else{
                        return "放弃";
                    }
                }
            }
            , {field: 'notice_time_str', width: 130, title: '通知时间', align: 'center'}
            , {fixed: 'right', width: 200, title: '功能操作', align: 'center', toolbar: '#toolBar'}
        ];

        //【渲染TABLE】
        func.tableIns(cols, "tableList", function (layEvent, data) {
            if (layEvent === 'pay') {
                var str = data.patent_name+",缴"+data.pay_time_str;
                if(data.end_time_str!=''){
                    str = str+'~'+data.end_time_str;
                }
                str = str+"费用";
                layer.confirm(str, function(index){
                    update_info(data.id,1)
                    layer.close(index);
                });       
            }
        });

        //【设置弹框】
        func.setWin("专利监控", 700, 500);

        $(".layui-btn-update").on("click",function(e){
            layer.confirm('确定执行?', function(index)
            {
                // 选择数据
                var checkStatus = table.checkStatus('tableList')
                        , data = checkStatus.data,ids='';
                if(data.length <= 0){
                    layer.msg("请选择数据")
                    return false;
                }
                for (da in data) {
                    ids += ids==''?data[da].id:','+data[da].id;
                }
                var type=$(e.currentTarget).attr("attr_type");
                update_info(ids,type)
                layer.close(index);
            });       
     
        })
        
        window.update_info = function (ids,type)
        {       
            $.ajax({
                type:'GET',
                url:'exeUpdate',
                data:{"ids":ids,'type':type},
                success:function(res){
                    data = JSON.parse(res)
                    layer.msg(data.msg)
                    if(data.code == 0){
                        // location.href = '/'+data.data['filename']
                        table.reload("tableList")
                    }
                }
            });
        }      


        $(".layui-btn-notice").on("click",function(e){
            var checkStatus = table.checkStatus('tableList')
                    , data = checkStatus.data,ids='',company_name='',self_pay='';

            if(data.length <= 0){
                layer.msg("请选择数据")
                return false;
            }
            for (da in data) {
                ids += ids==''?data[da].id:','+data[da].id;
                if(da == 0){
                    company_name = data[da].company_name
                    self_pay = data[da].self_pay
                }else{
                    if(company_name != data[da].company_name){
                        layer.msg("请选择同一个公司的数据")
                        return false;
                        break; 
                    }
                    if(self_pay != data[da].self_pay){
                        layer.msg("缴费类型不统一")
                        return false;
                        break; 
                    }
                }                    
                if(data[da].status != 1){
                    layer.msg("存在状态异常数据")
                    return false;
                    break; 
                }
            }

            var url = 'mkdoc?ids='+ids
            func.showWin("导出word文件", url);
        })

        $(".layui-btn-self").on("click",function(e){
            var checkStatus = table.checkStatus('tableList')
                    , data = checkStatus.data,ids='',company_name='',self_pay='';

            if(data.length <= 0){
                layer.msg("请选择数据")
                return false;
            }
            for (da in data) {
                ids += ids==''?data[da].id:','+data[da].id;
                if(da == 0){
                    company_name = data[da].company_name
                    self_pay = data[da].self_pay
                }else{
                    if(company_name != data[da].company_name){
                        layer.msg("请选择同一个公司的数据")
                        return false;
                        break; 
                    }
                }                    
                if(data[da].self_pay != 1){
                    layer.msg("缴费类型需要选择自缴")
                    return false;
                    break; 
                }
                if(data[da].status != 1){
                    layer.msg("存在状态异常数据")
                    return false;
                    break; 
                }
            }

            var url = 'self?ids='+ids
            func.showWin("导出客户自缴-办登转达函", url);
        })

        $(".layui-btn-company").on("click",function(e){
            var checkStatus = table.checkStatus('tableList')
                    , data = checkStatus.data,ids='',company_name='',self_pay='';

            if(data.length <= 0){
                layer.msg("请选择数据")
                return false;
            }
            for (da in data) {
                ids += ids==''?data[da].id:','+data[da].id;
                if(da == 0){
                    company_name = data[da].company_name
                    self_pay = data[da].self_pay
                }else{
                    if(company_name != data[da].company_name){
                        layer.msg("请选择同一个公司的数据")
                        return false;
                        break; 
                    }
                }                    
                if(data[da].self_pay != 0){
                    layer.msg("缴费类型需要选择公司缴费")
                    return false;
                    break; 
                }
                if(data[da].status != 1){
                    layer.msg("存在状态异常数据")
                    return false;
                    break; 
                }
            }

            var url = 'company?ids='+ids
            func.showWin("导出公司缴纳-办登转达函", url);
        })

        $(".layui-btn-year").on("click",function(e){
            var checkStatus = table.checkStatus('tableList')
                    , data = checkStatus.data,ids='',company_name='',self_pay='';

            if(data.length <= 0){
                layer.msg("请选择数据")
                return false;
            }
            for (da in data) {
                ids += ids==''?data[da].id:','+data[da].id;
                if(da == 0){
                    company_name = data[da].company_name
                    self_pay = data[da].self_pay
                }else{
                    if(company_name != data[da].company_name){
                        layer.msg("请选择同一个公司的数据")
                        return false;
                        break; 
                    }
                }                    
                if(data[da].status != 1){
                    layer.msg("存在状态异常数据")
                    return false;
                    break; 
                }
            }

            var url = 'year?ids='+ids
            func.showWin("导出年费转达函", url);
        })

    } else if(A!='edit'){
        form.on('submit(submitForm)', function (data) {
            var url = 'exportDoc';
            $.get(
                url, 
                data.field, 
                function (data) {  
                    if(data.code == 0){
                        location.href = '/uploads/'+data.data
                    } else{
                        layer.msg(data.msg)
                    }             
            }, 'json')
            return false;
        });
    }



    var inst = dropdown.render({});
    var flag;  //全局变量用于标识是否延时执行keyup事件

    function searchUp()
    {
        clearTimeout(flag);
        flag = setTimeout(function(){ 
            search();
        }, 300);
    }
    $("#selectInput").on('keyup',function(){
        searchUp();
    })

     function getChild(company_id){
        // 网络请求
        $.ajax({
            type: "POST",
            url: '/Ofcontract/index',
            data: {'company_id':company_id},
            dataType: "json",
            success: function (res) {
                let str = '<option value="">请选择合同编号</option>';
                if(res.code==0){
                    let da = res.data
                    if(da){
                        for(d in da){
                            str += '<option value="'+da[d].id+'">'+da[d].contract_no+'</option>';
                        }
                        $("#contract_id").html(str)
                    }
                }
                form.render('select');
            },
            error: function () {
                layer.msg("AJAX请求异常");
            }
        });    
    }
    window.search = function () 
    {
        var value = $("#selectInput").val()??'',pid=$("#selectPid").attr("attr_pid"),select_data='';
        $("#company_id").val('')
        $("#contract_id").html('')
        $.ajax({
           type:'GET',
           url:'/ofcompany/getList',
           data:{"name":value},
           success:function(res){
                // 返回成功的数据
                var da = JSON.parse(res),str = '';
                if(da.code==0){
                    inst.reload({ 
                        elem: '#selectInput'
                        ,show: true //重载即显示
                        ,data: da.data
                        ,click: function(obj){
                          this.elem.val(obj.title);
                          $("#company_id").val(obj.id)
                          getChild(obj.id)
                        }
                    });
                }
           }
        });
        
    }

});
